package leetcode.backtrace;

/**
 * @Description
 * @Author 26233
 * @Create 2021-03-31 11:24:53
 */

/*
给定一个二维网格和一个单词，找出该单词是否存在于网格中。

单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例:

board =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

给定 word = "ABCCED", 返回 true
给定 word = "SEE", 返回 true
给定 word = "ABCB", 返回 false
 

提示：

board 和 word 中只包含大写和小写英文字母。
1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/word-search
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */

public class LeetCode79_Exist {

    public boolean exist(char[][] board, String word) {

        if(word.length() > board.length * board[0].length) return false;

        int[][] visited = new int[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(exist(board, i, j, word,0, visited)){
                    return true;
                }
                visited = new int[board.length][board[0].length];
            }
        }
        return false;
    }

    public boolean exist(char[][] board, int m, int n, String word, int len, int[][] visited){
        if(m < 0 || m >= board.length) return false;
        if(n < 0 || n >= board[0].length) return false;

        if(board[m][n] == word.charAt(len) && visited[m][n] == 0){
            visited[m][n] = 1;
            if (len == word.length() - 1) return true;
            boolean f1 = exist(board, m + 1, n, word, len + 1, visited);
            boolean f2 = exist(board, m - 1, n, word, len + 1, visited);
            boolean f3 = exist(board, m, n + 1, word, len + 1, visited);
            boolean f4 = exist(board, m, n - 1, word, len + 1, visited);
            visited[m][n] = 0;
            return f1 || f2 || f3 || f4;
        }
        visited[m][n] = 0;
        return false;
    }

    public boolean exist22(char[][] board, int m, int n, String word, int len, int[][] visited){
        if(m < 0 || m >= board.length) return false;
        if(n < 0 || n >= board[0].length) return false;

        if(board[m][n] == word.charAt(len) && visited[m][n] == 0){
            visited[m][n] = 1;
            if (len == word.length() - 1) return true;
            boolean f1 = exist(board, m + 1, n, word, len + 1, visited);
            if (f1) return true;
            boolean f2 = exist(board, m - 1, n, word, len + 1, visited);
            if (f2) return true;
            boolean f3 = exist(board, m, n + 1, word, len + 1, visited);
            if (f3) return true;
            boolean f4 = exist(board, m, n - 1, word, len + 1, visited);
            if (f4) return true;
            visited[m][n] = 0;
        }
        return false;
    }

    public static void main(String[] args) {
//        char[][] board ={
//                {'A','B','C','E'},
//                {'S','F','C','S'},
//                {'A','D','E','E'}
//        };
//        char[][] board ={
//                {'A','B','C','E'},
//                {'S','F','E','S'},
//                {'A','D','E','E'}
//        };

        char[][] board ={
                {'a','a','b','a', 'a', 'a'},
                {'a','a','b','b', 'b', 'a'},
                {'a','a','a','a', 'b', 'a'},
                {'b','a','b','b', 'a', 'b'},
                {'a','b','b','a', 'a', 'a'},
                {'a','a','a','a', 'a', 'b'},
        };
        // String word = "ABCCED";

//        String word = "ABCESEEEFS";

        String word = "bbbaabbbbbab";
        boolean result = new LeetCode79_Exist().exist(board, word);
        System.out.println("result = " + result);

    }

}
